package acptTests;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import tokenizer.StringMessage;


public class PermissionsTest extends ServerTest{

	protected final int adminID = 1;
	protected int modID;
	protected int userID;
	
	protected int ForumID;
	protected int SubForumID;
	protected int TopicID;
	protected int ReplyID;
	
	@Before
	public void setUp()
	{	
		super.setUp();
	}
	

	public String send(String str)
	{
		return sp.processMessage(new StringMessage(str) , ch).toString();
	}
	
	public void init()
	{
		send("INIT_FORUM \0 superAdmin \0 myPass");
	}
	
	public String loginSuperAdmin()
	{
		return send("ADMIN_LOGIN \0 superAdmin \0 myPass");
	}
	
	public String logout()
	{
		return send("LOGOUT");
	}
	
	public String loginUser(String user , String pass ,int forumID)
	{
		String result = send("LOG_IN \0 "+ user +" \0 "+ pass + " \0" + forumID);
		if (result.startsWith("LOGGED_IN"))
		userID = Integer.parseInt(getWord(result.toString(), 1));
		return result;
	}
	
	public String register(String user , String pass , int forumID)
	{
		String result = send("REGISTER \0 " + user + "\0 " + pass +" \0" + "max@max.com"+Math.random() +" \0" + forumID);
		if (result.startsWith("REGISTERED"))
		userID = Integer.parseInt(getWord(result.toString(), 1));
		return result;
	}
	
	public String addForum(String name , int AdminID)
	{
		return send("ADD_FORUM \0"+name+"\0"+AdminID+"\0 1 \0 1 \0 1 \0 7 \0 1 \0 1 \0 1 \0 1");
	}
	
	public String addSubForum(String name , int forumID , int ModID)
	{
		return send("ADD_SUBFORUM \0"+name+"\0"+forumID+"\0"+ModID);
	}
	public String addTopic(String title , String body , int subID)
	{
		return send("ADD_TOPIC \0"+title+"\0"+body+"\0"+subID);
	}
	public String addReply(String body ,  int topID)
	{
		return send("ADD_REPLY \0"+body+"\0"+topID);
	}
	public String editReply(String body ,  int repID)
	{
		return send("EDIT_REPLY \0"+body+"\0"+repID);
	}
	
	public String editPost(String body ,  int postID)
	{
		return send("EDIT_POST \0"+body+"\0"+postID);
	}
	
	public String addModerator(int userid , int subforumID)
	{
		return send("ADD_MODERATOR \0"+userid+"\0"+subforumID);
	}
	
	public String removeModerator(int userid , int subforumID)
	{
		return send("REMOVE_MODERATOR \0"+userid+"\0"+subforumID);
	}
	
	public String addAdmin(int userid , int forumID)
	{
		return send("ADD_ADMIN \0"+userid+"\0"+forumID);
	}
	
	public void register_login(String user, String pass , int forum)
	{
		register( user ,pass  , forum);
		String User = loginUser( user ,pass , forum);
		userID = Integer.parseInt(getWord(User, 1));
	}
	
	public void init_forum_logout()
	{	
		init();
		loginSuperAdmin();
		int rand = (int) (Math.random() * 10000);
		String forum = addForum("Cars"+rand , adminID);
		ForumID = Integer.parseInt(getWord(forum, 1));
		logout();
		
	}
	
	/*public String addModerator()
	{
		
	}*/
	/******** Testing **********/
	
	@Test
	public void testLogout()
	{
		init();
		//Guest
		String ans1 = logout();
		assertTrue(ans1.startsWith("ERROR_LOG_OUT"));
		
		//admin
		loginSuperAdmin();
		String forum = addForum("Cars" , adminID);
		ForumID = Integer.parseInt(getWord(forum, 1));
		String ans2 = logout();
		assertTrue(ans2.startsWith("LOGGED_OUT"));
		
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans3 = logout();
		assertTrue(ans3.startsWith("LOGGED_OUT"));
		
		//moderator
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		String subforum = addSubForum("Cars" , ForumID , userID);
		SubForumID = Integer.parseInt(getWord(forum, 1));
		logout();
		loginUser("max" ,"1234" , ForumID);
		String ans4 = logout();
		assertTrue(ans4.startsWith("LOGGED_OUT"));
		
	}
	
	@Test
	public void testLogin()
	{
		init_forum_logout();
		//Guest
		register( "max" ,"1234" , ForumID);
		String User = loginUser( "max" ,"1234" , ForumID);
		userID = Integer.parseInt(getWord(User, 1));
		assertTrue(User.startsWith("LOGGED_IN"));
		//user
		String ans2 = loginUser("max" ,"1234" , ForumID);
		assertTrue(ans2.startsWith("ERROR_LOG_IN"));
		logout();
		loginSuperAdmin();
		String subforum = addSubForum("Cars" , ForumID , userID);
		addModerator(userID , SubForumID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//moderator
		loginUser("max" ,"1234" , ForumID);
		String ans3 = loginUser( "max" ,"1234" , ForumID);
		assertTrue(ans3.startsWith("ERROR_LOG_IN"));
		logout();
		//admin
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		String ans5 = loginUser("max" ,"1234" , ForumID);
		String ans4 = loginUser("max" ,"1234" , ForumID);
		assertTrue(ans4.startsWith("ERROR_LOG_IN"));
		
	}
	
	@Test 
	public void testAddForum()
	{
		init_forum_logout();
		//Guest
		String ans = addForum("Sports" , adminID);
		assertTrue(ans.startsWith("ERROR_FORUM_ADD"));
		
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans1 = addForum("Drugs" , adminID);
		assertTrue(ans1.startsWith("ERROR_FORUM_ADD"));
		logout();
		
		//moderator
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		String subforum = addSubForum("Cars" , ForumID , userID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		loginUser( "max" ,"1234" , ForumID);
		String ans2 = addForum("Green Card" , userID);
		assertTrue(ans2.startsWith("ERROR_FORUM_ADD"));
		logout();
		
		//admin
		loginSuperAdmin();
		String forum1 = addForum("Sea-Food" , userID);
		int fid = Integer.parseInt(getWord(forum1, 1));
		addAdmin( userID,fid );
		logout();
		loginUser( "max" ,"1234" , fid);
		String ans3 = addForum("Sea-Food2" , userID);
		assertTrue(ans3.startsWith("ERROR_FORUM_ADD"));
	}
	
	@Test
	public void testRegister()
	{
		init();
		//admin
		loginSuperAdmin();
		String forum = addForum("Cars101" , adminID);
		ForumID = Integer.parseInt(getWord(forum, 1));
		String ans = register("tembel" ,"297821" ,ForumID);
		assertTrue(ans.startsWith("ERROR_REGISTER"));
		logout();
		
		//Guest
		String ans1 = register("max" ,"1234" , ForumID);
		assertTrue(ans1.startsWith("REGISTERED"));
		
		//user
		loginUser("max" ,"1234" ,ForumID);
		userID = Integer.parseInt(getWord(ans1, 1));
		String ans2 = register("max" ,"1234" , ForumID);
		assertTrue(ans2.startsWith("ERROR_REGISTER"));
		logout();
		
		//moderator
		loginSuperAdmin();
		String subforum = addSubForum("Planes122" , ForumID , userID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		addModerator(userID , SubForumID);
		logout();
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = register("udi" ,"5678" , ForumID);
		assertTrue(ans3.startsWith("ERROR_REGISTER"));
		
	}
	
	@Test
	public void testAddSubForum()
	{
		init_forum_logout();
		//Guest
		String ans = addSubForum("BMW1" , ForumID , adminID);
		assertTrue(ans.startsWith("ERROR_ADD_SUBFORUM"));
		
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans1 = addSubForum("BMW1" , ForumID , userID);
		assertTrue(ans1.startsWith("ERROR_ADD_SUBFORUM"));
		logout();
		
		//moderator
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		String subforum = addSubForum("Planes" , ForumID , userID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		loginUser("max","1234",ForumID);
		String ans2 = addSubForum("BMW" , ForumID , userID);
		assertTrue(ans2.startsWith("ERROR_ADD_SUBFORUM"));
		logout();
		
		//admin
		loginSuperAdmin();
		String forum1 = addForum("Sea-Food" , userID);
		int fid = Integer.parseInt(getWord(forum1, 1));
		addAdmin(userID,fid);
		logout();
		loginUser( "max" ,"1234" , fid);
		String ans3 = addSubForum("Sea-Food-Places" , ForumID ,adminID);
		assertTrue(ans3.startsWith("ERROR_ADD_SUBFORUM"));
	}
	
	@Test
	public void testAddTopic()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//guest
		String ans = addTopic("title", "Thread Body", SubForumID);
		assertTrue(ans.startsWith("ERROR_ADD_TOPIC"));
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans1 = addTopic("BMW" , "Engine type" , SubForumID);
		assertTrue(ans1.startsWith("TOPIC_ADDED"));
		logout();
		
		//moderator
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		loginUser("max","1234",ForumID);
		String ans2 = addTopic("BMW" , "Engine type" , SubForumID);
		assertTrue(ans2.startsWith("TOPIC_ADDED"));
		logout();
		
		//admin
		loginSuperAdmin();
		addAdmin( userID,ForumID);
		logout();
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = addTopic("BMW" , "Engine type" , SubForumID);
		assertTrue(ans3.startsWith("TOPIC_ADDED"));
	}
	
	@Test
	public void testAddReply()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		String topic = addTopic("title" , "body" ,SubForumID);
		TopicID = Integer.parseInt(getWord(topic, 1));
		logout();
		//guest
		String ans = addReply("Reply Body", TopicID);
		assertTrue(ans.startsWith("ERROR_ADD_REPLY"));
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans1 = addReply("Engine type" , TopicID);
		assertTrue(ans1.startsWith("REPLY_ADDED"));
		logout();
		
		//moderator
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		loginUser("max","1234",ForumID);
		String ans2 = addReply("Engine type" , TopicID);
		assertTrue(ans2.startsWith("REPLY_ADDED"));
		logout();
		
		//admin
		loginSuperAdmin();
		addAdmin( userID,ForumID);
		logout();
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = addReply("Engine type" , TopicID);
		assertTrue(ans3.startsWith("REPLY_ADDED"));
	}
	
	@Test
	public void testShowForums()
	{
		System.out.println("testing...");
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		//guest
		String ans = send("SHOW_FORUMS");
		assertTrue(ans.startsWith("ANS_SHOW_FORUMS"));
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans3 = send("SHOW_FORUMS");
		assertTrue(ans3.startsWith("ANS_SHOW_FORUMS"));
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = send("SHOW_FORUMS");
		assertTrue(ans4.startsWith("ANS_SHOW_FORUMS"));
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans5 = send("SHOW_FORUMS");
		assertTrue(ans5.startsWith("ANS_SHOW_FORUMS"));
	}
	
	public void testShowSubForums()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		//guest
		String ans = send("SHOW_SUBFORUMS\0"+ForumID);
		assertTrue(ans.startsWith("ANS_SHOW_SUBFORUMS"));
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans3 = send("SHOW_SUBFORUMS\0"+ForumID);
		assertTrue(ans3.startsWith("ANS_SHOW_SUBFORUMS"));
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = send("SHOW_SUBFORUMS\0"+ForumID);
		assertTrue(ans4.startsWith("ANS_SHOW_SUBFORUMS"));
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans5 = send("SHOW_SUBFORUMS\0"+ForumID);
		assertTrue(ans5.startsWith("ANS_SHOW_SUBFORUMS"));
	}
	
	public void testShowTopics()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		String topic = addTopic("hey there","whats up with you?",SubForumID);
		logout();
		
		//guest
		String ans = send("SHOW_TOPICS\0"+SubForumID);
		assertTrue(ans.startsWith("ANS_SHOW_TOPICS"));
		//user
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = send("SHOW_TOPICS\0"+SubForumID);
		assertTrue(ans3.startsWith("ANS_SHOW_TOPICS"));
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = send("SHOW_TOPICS\0"+SubForumID);
		assertTrue(ans4.startsWith("ANS_SHOW_TOPICS"));
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans5 = send("SHOW_TOPICS\0"+SubForumID);
		assertTrue(ans5.startsWith("ANS_SHOW_TOPICS"));
	}
	
	@Test
	public void testShowAllUsers()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes2" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//guest
		String ans = send("SHOW_SYSTEM_USERS");
		assertTrue(ans.startsWith("ERROR_SHOW_SYSTEM_USERS"));
		
		register_login( "max" ,"1234" , ForumID);
		//user
		String ans1 = send("SHOW_SYSTEM_USERS");
		assertTrue(ans1.startsWith("ERROR_SHOW_SYSTEM_USERS"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans2 = send("SHOW_SYSTEM_USERS");
		assertTrue(ans2.startsWith("ERROR_SHOW_SYSTEM_USERS"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = send("SHOW_SYSTEM_USERS");
		assertTrue(ans3.startsWith("ERROR_SHOW_SYSTEM_USERS"));
	}
	
	public void testRemoveTopic()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		String topic = addTopic("hey there","whats up with you?",SubForumID);
		System.out.println("##################" + topic);
		TopicID = Integer.parseInt(getWord(topic, 1));
		logout();
		
		//guest
		String ans = send("REMOVE_TOPIC\0"+TopicID);
		assertTrue(ans.startsWith("ERROR_REMOVE_TOPIC"));
		//user
		register_login( "max" ,"1234" , ForumID);
		String ans3 = send("REMOVE_TOPIC\0"+TopicID);
		assertTrue(ans3.startsWith("ERROR_REMOVE_TOPIC"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = send("REMOVE_TOPIC\0"+TopicID);
		System.out.println("---------");
		System.out.println(ans4);
		assertTrue(ans4.startsWith("TOPIC_REMOVED"));
		logout();
		
		loginSuperAdmin();
		addAdmin(1 , 1);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		System.out.println("=============");
		String ans5 = send("REMOVE_TOPIC\0"+TopicID);
	
		
		System.out.println(ans5);
		System.out.println("---------");
		assertTrue(ans5.startsWith("ERROR_REMOVE_TOPIC"));
	}
	
	@Test
	public void testAddModerator()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		logout();
		//guest
		String ans = addModerator(userID , SubForumID);
		assertTrue(ans.startsWith("ERROR_ADD_MODERATOR"));
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = addModerator(usr , SubForumID);
		assertTrue(ans3.startsWith("ERROR_ADD_MODERATOR"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = addModerator(usr , SubForumID);
		assertTrue(ans4.startsWith("ERROR_ADD_MODERATOR"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = addModerator(usr , SubForumID);
		System.out.println("====================");
		System.out.println(ans5);
		assertTrue(ans5.startsWith("MODERATOR_ADDED"));
		logout();
	}
	
	@Test
	public void testRemoveSubForum()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//guest
		String ans = send("REMOVE_SUBFORUM\0"+SubForumID);
		assertTrue(ans.startsWith("ERROR_REMOVE_SUBFORUM"));
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = send("REMOVE_SUBFORUM\0"+SubForumID);
		assertTrue(ans3.startsWith("ERROR_REMOVE_SUBFORUM"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = send("REMOVE_SUBFORUM\0"+SubForumID);
		assertTrue(ans4.startsWith("ERROR_REMOVE_SUBFORUM"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = send("REMOVE_SUBFORUM\0"+SubForumID);
		assertTrue(ans5.startsWith("SUBFORUM_REMOVED"));
		logout();
	}
	
	@Test
	public void testRemoveModerator()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		logout();
		loginSuperAdmin();
		addModerator(usr,SubForumID);
		logout();
		//guest
		String ans = removeModerator(userID , SubForumID);
		assertTrue(ans.startsWith("ERROR_REMOVE_MODERATOR"));
		
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = removeModerator(usr , SubForumID);
		assertTrue(ans3.startsWith("ERROR_REMOVE_MODERATOR"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = removeModerator(usr , SubForumID);
		assertTrue(ans4.startsWith("ERROR_REMOVE_MODERATOR"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = removeModerator(usr , SubForumID);
		assertTrue(ans5.startsWith("MODERATOR_REMOVED"));
		logout();
	}
	
	@Test
	public void testNumOfTopicsInSubforum()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		String topic = addTopic("hey there","whats up with you?",SubForumID);
		TopicID = Integer.parseInt(getWord(topic, 1));
		logout();
		
		//guest
		String ans = send("GET_SUBFORUM_SIZE\0"+SubForumID);
		assertTrue(ans.startsWith("ANS_GET_SUBFORUM_SIZE"));
		
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = send("GET_SUBFORUM_SIZE\0"+SubForumID);
		assertTrue(ans3.startsWith("ANS_GET_SUBFORUM_SIZE"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = send("GET_SUBFORUM_SIZE\0"+SubForumID);
		assertTrue(ans4.startsWith("ANS_GET_SUBFORUM_SIZE"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = send("GET_SUBFORUM_SIZE\0"+SubForumID);
		assertTrue(ans5.startsWith("ANS_GET_SUBFORUM_SIZE"));
		logout();
	}
	
	@Test
	public void testNumOfPostsOfUser()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		addTopic("well well","what have we here",SubForumID);
		logout();
		
		//guest
		String ans = send("GET_USER_MSGCOUNT\0"+usr);
		System.out.println("============");
		System.out.println(ans);
		assertTrue(ans.startsWith("ANS_GET_USER_MSGCOUNT"));
		
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = send("GET_USER_MSGCOUNT\0"+usr);
		assertTrue(ans3.startsWith("ANS_GET_USER_MSGCOUNT"));
		logout();
		
		loginUser("max" ,"1234" , ForumID);
		String ans8 = send("GET_USER_MSGCOUNT\0"+usr);
		assertTrue(ans8.startsWith("ANS_GET_USER_MSGCOUNT"));
		logout();
				
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = send("GET_USER_MSGCOUNT\0"+usr);
		assertTrue(ans4.startsWith("ANS_GET_USER_MSGCOUNT"));
		logout();
				
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = send("GET_USER_MSGCOUNT\0"+usr);
		System.out.println("========");
		System.out.println(ans5);
		assertTrue(ans5.startsWith("ANS_GET_USER_MSGCOUNT"));
		logout();
	}
	
	@Test
	public void testUsersRepliedToUser()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		String top = addTopic("well well","what have we here",SubForumID);
		int topicID = Integer.parseInt(getWord(top,1));
		logout();
		register_login( "sam" ,"5678" , ForumID);
		addReply("what did you say?" , topicID);
		logout();
		
		//guest
		String ans = send("GET_AUTHORS_OF_REPLIES_TO_USER\0"+usr);
		assertTrue(ans.startsWith("ERROR_GET_AUTHORS_OF_REPLIES_TO_USER"));
				
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = send("GET_AUTHORS_OF_REPLIES_TO_USER\0"+usr);
		assertTrue(ans3.startsWith("ERROR_GET_AUTHORS_OF_REPLIES_TO_USER"));
		logout();
				
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = send("GET_AUTHORS_OF_REPLIES_TO_USER\0"+usr);
		assertTrue(ans4.startsWith("ERROR_GET_AUTHORS_OF_REPLIES_TO_USER"));
		logout();
				
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = send("GET_AUTHORS_OF_REPLIES_TO_USER\0"+usr);
		//assertTrue(ans5.startsWith("ANS_GET_AUTHORS_OF_REPLIES_TO_USER"));
		logout();
	}
	
	@Test
	public void testNumOfForums()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//guest
		String ans = send("GET_FORUM_COUNT");
		assertTrue(ans.startsWith("ANS_GET_FORUM_COUNT"));
		
		register_login( "max" ,"1234" , ForumID);
		//user
		String ans1 = send("GET_FORUM_COUNT");
		assertTrue(ans1.startsWith("ANS_GET_FORUM_COUNT"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans2 = send("GET_FORUM_COUNT");
		assertTrue(ans2.startsWith("ANS_GET_FORUM_COUNT"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = send("GET_FORUM_COUNT");
		assertTrue(ans3.startsWith("ANS_GET_FORUM_COUNT"));
	}
	
	@Test
	public void testUsersRegisteredToMultipleForums()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		//guest
		String ans = send("GET_COMMON_USERS");
		assertTrue(ans.startsWith("ERROR_GET_COMMON_USERS"));
		
		register_login( "max" ,"1234" , ForumID);
		//user
		String ans1 = send("GET_COMMON_USERS");
		assertTrue(ans1.startsWith("ERROR_GET_COMMON_USERS"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser( "max" ,"1234" , ForumID);
		String ans2 = send("GET_COMMON_USERS");
		assertTrue(ans2.startsWith("ERROR_GET_COMMON_USERS"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans3 = send("GET_COMMON_USERS");
		assertTrue(ans3.startsWith("ERROR_GET_COMMON_USERS"));
	}
	
	@Test
	public void testShowForumUsers()
	{
			init_forum_logout();
			loginSuperAdmin();
			String subforum = addSubForum("Planes" , ForumID , adminID);
			SubForumID = Integer.parseInt(getWord(subforum, 1));
			logout();
			
			//guest
			String ans = send("SHOW_FORUM_USERS\0"+ForumID);
			System.out.println("=========");
			System.out.println(ans);
			assertTrue(ans.startsWith("ANS_SHOW_FORUM_USERS"));
			//user
			register_login( "max" ,"1234" , ForumID);
			String ans3 = send("SHOW_FORUM_USERS\0"+ForumID);
			assertTrue(ans3.startsWith("ANS_SHOW_FORUM_USERS"));
			
			loginSuperAdmin();
			addModerator(userID , SubForumID);
			logout();
			//moderator
			loginUser( "max" ,"1234" , ForumID);
			String ans4 = send("SHOW_FORUM_USERS\0"+ForumID);
			assertTrue(ans4.startsWith("ANS_SHOW_FORUM_USERS"));
			
			loginSuperAdmin();
			addAdmin(userID , ForumID);
			logout();
			//admin
			loginUser( "max" ,"1234" , ForumID);
			String ans5 = send("SHOW_FORUM_USERS\0"+ForumID);
			assertTrue(ans5.startsWith("ANS_SHOW_FORUM_USERS"));
	}
	
	@Test
	public void testEditReply()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		String top = addTopic("well well","what have we here",SubForumID);
		int topicID = Integer.parseInt(getWord(top,1));
		logout();
		register_login( "sam" ,"5678" , ForumID);
		String rep = addReply("what did you say?" , topicID);
		int replyID = Integer.parseInt(getWord(rep,1));
		logout();
		
		//guest
		String ans = editReply("Oh I got you!" , replyID);
		assertTrue(ans.startsWith("ERROR_EDIT_REPLY"));
		
		//user
		loginUser("max" ,"1234" ,ForumID);
		String ans1 = editReply("Oh I got you!" , replyID);
		assertTrue(ans1.startsWith("ERROR_EDIT_REPLY"));
		logout();
		loginUser("sam" ,"5678" ,ForumID);
		String ans2 = editReply("Oh I got you!" , replyID);
		assertTrue(ans2.startsWith("REPLY_EDITED"));
		logout();
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		
		//moderator
		loginUser( "sam" ,"5678" , ForumID);
		String ans3 = editReply("Oh I got you!" , replyID);
		assertTrue(ans3.startsWith("REPLY_EDITED"));
		logout();
		
		loginSuperAdmin();
		addAdmin(usr , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = editReply("Oh I got you!" , replyID);
		assertTrue(ans4.startsWith("REPLY_EDITED"));
		logout();
	}
	
	@Test
	public void testAddAdmin()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		logout();
		//guest
		String ans = addAdmin(userID , ForumID);
		assertTrue(ans.startsWith("ERROR_ADD_ADMIN"));
		//user
		register_login( "maxi" ,"4321" , ForumID);
		String ans3 = addAdmin(usr , ForumID);
		assertTrue(ans3.startsWith("ERROR_ADD_ADMIN"));
		logout();
		
		loginSuperAdmin();
		addModerator(userID , SubForumID);
		logout();
		//moderator
		loginUser("maxi" , "4321" ,ForumID);
		String ans4 = addAdmin(usr , ForumID);
		assertTrue(ans4.startsWith("ERROR_ADD_ADMIN"));
		logout();
		
		loginSuperAdmin();
		addAdmin(userID , ForumID);
		logout();
		//admin
		loginUser("maxi" , "4321" ,ForumID);
		String ans5 = addAdmin(usr , ForumID);
		assertTrue(ans5.startsWith("ERROR_ADD_ADMIN"));
		logout();
	}
	
	@Test
	public void testEditPost()
	{
		init_forum_logout();
		loginSuperAdmin();
		String subforum = addSubForum("Planes" , ForumID , adminID);
		SubForumID = Integer.parseInt(getWord(subforum, 1));
		logout();
		
		register_login( "max" ,"1234" , ForumID);
		int usr = userID;
		String top = addTopic("well well","what have we here",SubForumID);
		int topicID = Integer.parseInt(getWord(top,1));
		logout();
		register_login( "sam" ,"5678" , ForumID);
		logout();
		
		//guest
		String ans = editPost("Oh I got you!" , topicID);
		assertTrue(ans.startsWith("ERROR_EDIT_POST"));
		
		
		//user
		loginUser("max" ,"1234" ,ForumID);
		String ans1 = editPost("Oh I got you!" , topicID);
		assertTrue(ans1.startsWith("POST_EDITED"));
		logout();
		loginUser("sam" ,"5678" ,ForumID);
		int user = userID;
		String ans2 = editPost("What is it?!" , topicID);
		assertTrue(ans2.startsWith("ERROR_EDIT_POST"));
		logout();
		loginSuperAdmin();
		addModerator(user , SubForumID);
		logout();
		
		//moderator
		loginUser( "sam" ,"5678" , ForumID);
		String ans3 = editPost("Oh I got you!" , topicID);
		assertTrue(ans3.startsWith("POST_EDITED"));
		logout();
		
		loginSuperAdmin();
		addAdmin(usr , ForumID);
		logout();
		//admin
		loginUser( "max" ,"1234" , ForumID);
		String ans4 = editPost("Oh I got you!" , topicID);
		assertTrue(ans4.startsWith("POST_EDITED"));
		logout();
	}
}
